package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class SpuMangeController {

    @Autowired
    private ManageService manageService;

    //SpringMVC 对象传值:传递过来的参数与实体类的属性名称保持一致,则可以使用实体类接收
    //http://api.gmall.com/admin/product/ {page}/{limit}?category3Id=61
    //获取spu分页列表
    @GetMapping("{page}/{limit}")
    public Result getSpuList(@PathVariable Long page,
                             @PathVariable Long limit,
//                             HttpServletRequest request,
//                             String category3Id,
                             SpuInfo spuInfo){
        //处理的分页查询
        //IPage --> Page
        Page<SpuInfo> spuInfopage = new Page<SpuInfo>(page, limit);
        //调用服务层方法
        IPage spuInfoPageList =  manageService.getSpuInfoList(spuInfopage,spuInfo);
        //返回数据
        return Result.ok(spuInfoPageList);
    }

    //获取销售属性
    //http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public  Result baseSaleAttrList(){
        //返回销售属性集合
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //todo 第四天
    //大保存
    //添加spu
    //http://api.gmall.com/admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public  Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);

        return Result.ok();
    }

    //获取spuId的图片列表
    //http://api.gmall.com/admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok();
    }

}
